/*
 * Copyright 2011 yura.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.opu.db_vdumper.actions.db;

import java.util.LinkedHashMap;
import java.util.Map;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import org.opu.db_vdumper.actions.AbstractAction;
import org.opu.db_vdumper.util.DbManager;
import org.opu.db_vdumper.util.Logger;

/**
 *
 * @author yura
 */
public abstract class DbAction extends AbstractAction {

    public static final String DB_ACTION_NAME = "DbAction";
    public static final String COMMIT = "Commit";
    public static final String ROLLBACK = "Rollback";
    public static final String AUTOCOMMIT = "Autocommit";
    protected DbManager dbManager;

    public DbAction(DbManager dbManager) {
        setDbManager(dbManager);
        putValue(LARGE_ICON_KEY, null);
    }

    public DbAction() {
        this(null);
    }

    public DbManager getDbManager() {
        return dbManager;
    }

    public void setDbManager(DbManager dbManager) {
        this.dbManager = dbManager;
        setEnabled(dbManager != null);
    }

    protected void errorNoConnect() {
        Logger.getInstance().info(DbAction.class, "No connect to database");

        try {
            JOptionPane.showMessageDialog(null,
                    boundle.getString(DB_ACTION_NAME + ".Text.noconnect"),
                    boundle.getString(DB_ACTION_NAME + ".Title.noconnect"),
                    JOptionPane.ERROR_MESSAGE);
        } catch (HeadlessException ex) {
            Logger.getInstance().error(DbAction.class, ex);
        }
    }

    public static Map<String, DbAction> getActions(DbManager dbManager) {
        Map<String, DbAction> actions = new LinkedHashMap<String, DbAction>();
        final DbAction commit = new DbAction(dbManager) {

            @Override
            public String getName() {
                return COMMIT;
            }

            @Override
            public void action(ActionEvent e) {
                if (dbManager != null) {
                    dbManager.commit();
                } else {
                    errorNoConnect();
                }
            }

            @Override
            public void setDbManager(DbManager dbManager) {
                super.setDbManager(dbManager);
                if (isEnabled() && dbManager != null) {
                    setEnabled(!dbManager.isAutoCommit());
                }
            }
        };

        final DbAction rollback = new DbAction(dbManager) {

            @Override
            public String getName() {
                return ROLLBACK;
            }

            @Override
            public void action(ActionEvent e) {
                if (dbManager != null) {
                    dbManager.rollback();
                } else {
                    errorNoConnect();
                }
            }

            @Override
            public void setDbManager(DbManager dbManager) {
                super.setDbManager(dbManager);
                if (isEnabled() && dbManager != null) {
                    setEnabled(!dbManager.isAutoCommit());
                }
            }
        };

        DbAction autocommit = new DbAction(dbManager) {

            @Override
            public String getName() {
                return AUTOCOMMIT;
            }

            @Override
            public void action(ActionEvent e) {
                if (dbManager != null) {
                    boolean b = dbManager.setAutoCommit(!dbManager.isAutoCommit());
                    commit.setEnabled(!b);
                    rollback.setEnabled(!b);
                    Logger.getInstance().info(DbAction.class, "Auto commit " + b);
                } else {
                    errorNoConnect();
                }
            }
        };

        actions.put(ExecuteQueryAction.MNAME, new ExecuteQueryAction(dbManager));
        actions.put(COMMIT, commit);
        actions.put(ROLLBACK, rollback);
        actions.put(AUTOCOMMIT, autocommit);

        return actions;
    }

    public static Map<String, DbAction> getActions() {
        return getActions(null);
    }
}
